#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include"physics.c"
int main ()
{
	int continuer =1,imageactu=0,i=0,direction=0;
	SDL_Surface * tab_left[5];
	SDL_Surface * tab_right[5];
       
int x;int y; int R;int G;int B;
	

	SDL_Surface *surface = NULL,* BackgroundCol=NULL,*ecran = NULL,*hero1 =NULL,*hero2 =NULL,*hero3 =NULL,*hero4 =NULL,*hero5 =NULL,*hero6 =NULL,*hero7 =NULL,*hero8 =NULL,*v1=NULL,*v2=NULL,*argent=NULL,*argent1=NULL;

	SDL_Rect positionecran,positionhero1,positionv1,positionv2,positionarg,poscam,positionarg1;
	SDL_Event event ;

	positionecran.x=0;
	positionecran.y=0;

	positionhero1.x=50;
	positionhero1.y=200;


	positionv1.x=600;
	positionv1.y=470;

	positionv2.x=150;
	positionv2.y=470;

	positionarg.x=40;
	positionarg.y=80;

	positionarg1.x=150;
	positionarg1.y=350;

	poscam.x=0;
	poscam.y=0;
	poscam.w=900;
	poscam.h=600;

	SDL_Init(SDL_INIT_VIDEO);

	ecran = SDL_SetVideoMode(900, 600, 32, SDL_HWSURFACE);

	SDL_WM_SetCaption("Enki Sidi Bou Said", NULL);

	surface=SDL_LoadBMP("back1.bmp");
        BackgroundCol=SDL_LoadBMP("bacck1.bmp");

	v1=SDL_LoadBMP("v1.bmp");
	SDL_SetColorKey(v1, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(v1->format, 255, 255, 255));

	v2=SDL_LoadBMP("v2.bmp");
	SDL_SetColorKey(v2, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(v2->format, 255, 255, 255));


	argent=SDL_LoadBMP("dinar.bmp");
	SDL_SetColorKey(argent, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(argent->format, 255, 255, 255));

	argent1=SDL_LoadBMP("index.bmp");
	SDL_SetColorKey(argent1, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(argent1->format, 255, 255, 255));


	tab_right[0]=SDL_LoadBMP("3.bmp");
	tab_right[1]=SDL_LoadBMP("5.bmp");
	tab_right[2]=SDL_LoadBMP("6.bmp");

	tab_left[0]=SDL_LoadBMP("4.bmp");
	tab_left[1]=SDL_LoadBMP("1.bmp");
	tab_left[2]=SDL_LoadBMP("11.bmp");
	
	positionhero1.w=tab_right[0]->w;
	positionhero1.h=tab_right[0]->h; //onrécupere la largeur et la hauteur des persos


	
	SDL_SetColorKey(tab_right[0], (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(tab_right[0]->format, 255, 255, 255));
	SDL_SetColorKey(tab_right[1], (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(tab_right[1]->format, 255, 255, 255));
	SDL_SetColorKey(tab_right[2], (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(tab_right[2]->format, 255, 255, 255));
	SDL_SetColorKey(tab_left[0], (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(tab_left[0]->format, 255, 255, 255));
	SDL_SetColorKey(tab_left[1], (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(tab_left[1]->format, 255, 255, 255));
	SDL_SetColorKey(tab_left[2], (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(tab_left[2]->format, 255, 255, 255));

//SDL_BlitSurface(hero1, NULL , ecran, &positionhero1);
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
//SDL_Flip(ecran);

while (continuer) 
{
		while(SDL_PollEvent(&event)) 
                 {

			switch(event.type) 
                       {
			case SDL_QUIT:
				continuer=0;
				break;
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) 
                                {
				case SDLK_RIGHT:
					if(!collideBackgroundRight(BackgroundCol,positionhero1,poscam) )
					{i++;
					direction=0;
					poscam.x+=5;
                                         }

					break;
				case SDLK_LEFT:
            if(!collideBackgroundLeft(BackgroundCol,positionhero1,poscam))
					{i++;
					direction=1;
					poscam.x-=5;
                                        }
					break;
				case SDLK_UP:
     if(!collideBackgroundUp(BackgroundCol,positionhero1,poscam))  
					{direction=2;
					positionhero1.y-=6;}
					break;
				/*case SDLK_DOWN:
     if(!collideBackgroundDown(BackgroundCol,positionhero1,poscam))
					{direction=3;
					positionhero1.y+=1;}
					break;*/

				}
				break;
			 case SDL_KEYUP:
				direction=3;
				i=0;
				break;
			}
		}//end event

		 if(!collideBackgroundDown(BackgroundCol,positionhero1,poscam))
					{
					positionhero1.y+=1;}

		//SDL_BlitSurface(surface,&poscam,ecran,NULL);
		SDL_BlitSurface(surface,&poscam,ecran,NULL);
		SDL_BlitSurface(argent, NULL , ecran, &positionarg);
		SDL_BlitSurface(argent1, NULL , surface, &positionarg1);
		SDL_BlitSurface(v2, NULL , surface, &positionv2);
		SDL_BlitSurface(v1, NULL , surface, &positionv1);

		
               
                if(direction==0)
                   { 
                    
			SDL_BlitSurface (tab_right[i] ,NULL, ecran, &positionhero1);}
     
		if(direction==1)
                    {
                      
			  SDL_BlitSurface (tab_left[i] ,NULL, ecran, &positionhero1);}
                
                if(direction==2)
                  { 
                        
			SDL_BlitSurface (tab_right[i] ,NULL, ecran, &positionhero1);}
                       
                        

		if(direction==3)
            {
               
			SDL_BlitSurface (tab_right[i] ,NULL, ecran, &positionhero1);}

		SDL_Flip(ecran);
		
		
		i=i%3;

	}

	SDL_Quit();

	return EXIT_SUCCESS;
}
